# coending:utf8
import inspect
import os
import time
from InterfaceAuto.commonalKeys import syspublic
from appAuto.appkeys import App
from configuration import config, Encrypt
from configuration.excelresult import Res
from configuration.logger import logger
from InterfaceAuto.InterKeys import HTTP
from configuration.mail import Mail
from configuration.mysql import Mysql
from configuration.newExcel import Writer, Reader
from configuration.txt import Txt
from run.fun import runcase
from webAuto.webKeys import Web


"""

    AutoFramework System
    Data driven Run main
    Creator : Dou

"""
syspublic.path=os.path.dirname(__file__)

logger.info("------开始运行-----")

# 初始化配置
config.get_config('./conf/conf.properties')

# 初始化数据库
sql = Mysql()
sql.init_mysql('./conf/initialize.sql')

# 启用加密
# Encrypt.init()

casename = 'API用例1'

# 读数据
reader = Reader()
reader.open_excel('./testCase/%s.xlsx' % casename)
writer = Writer()
writer.copy_open('./testCase/%s.xlsx' % casename, './testCase/result/result-%s.xlsx' % casename)
sheetname = reader.get_sheets()

# 写开始时间
starttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
writer.set_sheet(sheetname[0])
writer.write(1, 9, starttime)

# 默认运行HTTP
http = HTTP(writer)
web = Web(writer)
app = App(writer)
obj = http


for sheet in sheetname:
    # 设置当前读取的sheet页面
    reader.set_sheet(sheet)
    writer.set_sheet(sheet)

    # xls
    # for i in range(reader.rows):
    #     bj.row = i
    #     line = reader.readline()
    #     runcase(bj, line)

    # openpyxl
    lines = reader.readline()

    # 定义Excel第一行单元格填的是什么就运行什么
    line = lines[1]
    if line[1] == "HTTP":
        obj = http
    elif line[1] == "WEB":
        obj = web
    elif line[1] == "APP":
        obj = app

    for i in range(reader.rows):
        obj.row = i
        line = lines[i]
        logger.info(line)
        runcase(obj, line)

# 结束时间
endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
writer.set_sheet(writer.get_sheets()[0])
writer.write(1, 10, endtime)
writer.save_close()

# 获取结果数据
result = Res()
summary = result.get_res('./testCase/result/result-%s.xlsx' % casename)
logger.info(summary)
groups = result.get_groups('./testCase/result/result-%s.xlsx' % casename)
logger.info(groups)

# 读取邮件配置模板
txt = Txt('./conf/' + config.config['mailtxt'])
html = txt.read()[0]

# 替换汇总信息
for key in summary.keys():
    if summary[key] == "Pass":
        html = html.replace(
            '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;">status</td>',
            '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;color:blue;">Pass</td>')
    elif summary[key] == "Fail":
        html = html.replace(
            '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;">status</td>',
            '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;color:red;">Fail</td>')
    else:
        html = html.replace(key, summary[key])

# 获取分组信息
tr = '<tr><td width="100" height="28" ' \
     'align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28"' \
     'align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" ' \
     'align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" ' \
     'align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">状态</td></tr>'
trs = ''

for i in range(len(groups)):
    tmp = tr.replace('分组信息', str(groups[i][0]))
    tmp = tmp.replace('用例总数', str(groups[i][1]))
    tmp = tmp.replace('通过数', str(groups[i][2]))
    tmp = tmp.replace('状态', str(groups[i][3]))
    trs += tmp

html = html.replace('mailbody', trs)
mail = Mail()
mail.send(html)

# 关闭加密
# Encrypt.shutdown()
